﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;

namespace System {
    /// <summary>
    /// 安全帮助类，提供SHA-1算法等
    /// </summary>
    public partial class EncryptHelper {

        #region SHA相关

        /// <summary>
        /// 采用SHA-1算法加密字符串（小写）
        /// </summary>
        /// <param name="encypStr">需要加密的字符串</param>
        /// <returns></returns>
        public static string GetSha1(string encypStr) {
            var sha1 = SHA1.Create();
            var sha1Arr = sha1.ComputeHash(Encoding.UTF8.GetBytes(encypStr));
            StringBuilder enText = new StringBuilder();
            foreach(var b in sha1Arr) {
                enText.AppendFormat("{0:x2}",b);
            }
            return GetSha1(encypStr,false);
        }

        /// <summary>
        /// 采用SHA-1算法加密字符串（默认大写）
        /// </summary>
        /// <param name="encypStr">需要加密的字符串</param>
        /// <param name="toUpper">是否返回大写结果，true：大写，false：小写</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string GetSha1(string encypStr,bool toUpper = true,Encoding encoding = null) {
            encoding=Encoding.UTF8;
            var sha1 = SHA1.Create();
            var sha1Arr = sha1.ComputeHash(encoding.GetBytes(encypStr));
            StringBuilder enText = new StringBuilder();
            foreach(var b in sha1Arr) {
                enText.AppendFormat("{0:x2}",b);
            }
            if(!toUpper) {
                return enText.ToString();
            } else {
                return enText.ToString().ToUpper();
            }
        }

        /// <summary>
        /// 采用SHA-1算法加密流（默认大写）
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="toUpper">是否返回大写结果，true：大写，false：小写</param>
        /// <returns></returns>
        public static string GetSha1(Stream stream,bool toUpper = true) {
            stream.Seek(0,SeekOrigin.Begin);

            var sha1 = SHA1.Create();
            var sha1Arr = sha1.ComputeHash(stream);
            StringBuilder enText = new StringBuilder();
            foreach(var b in sha1Arr) {
                enText.AppendFormat("{0:x2}",b);
            }
            var result = enText.ToString();
            if(!toUpper) {
                return result;
            } else {
                return result.ToUpper();
            }
        }
        /// <summary>
        /// HMAC SHA256 加密
        /// </summary>
        /// <param name="message">加密消息原文。当为小程序SessionKey签名提供服务时，其中message为本次POST请求的数据包（通常为JSON）。特别地，对于GET请求，message等于长度为0的字符串。</param>
        /// <param name="secret">秘钥（如小程序的SessionKey）</param>
        /// <returns></returns>
        public static string GetHmacSha256(string message,string secret) {
            message=message??"";
            secret=secret??"";
            byte[] keyByte = Encoding.UTF8.GetBytes(secret);
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);
            using(var hmacsha256 = new HMACSHA256(keyByte)) {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                StringBuilder enText = new StringBuilder();
                foreach(var b in hashmessage) {
                    enText.AppendFormat("{0:x2}",b);
                }
                return enText.ToString();
            }
        }
        #endregion
    }
}
